package study.alex.resumedb.gae.model;

import org.datanucleus.api.jpa.annotations.Extension;
import study.alex.resumedb.gae.EMF;

import javax.persistence.*;
import java.io.Serializable;
import java.util.*;
import java.util.logging.Logger;

/**
 * Created with IntelliJ IDEA.
 * User: alex
 * Date: 07.11.13
 * Time: 12:09
 * SVN $Id$
 */


@Entity
public class Contact implements Serializable {

    private final static Logger log = Logger.getLogger(Contact.class.getName());

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
    private String id;

    private String userId = null;

    private String name = null;
    private String address = null;

    private String phones = "";
    private String emails = "";
    private Boolean personflag = true;
    private String organization = null;
    private String comment = null;
    private String urlinfoapi;
    private String urlinfoweb;
    private Set<String> vacancies = new HashSet<String>();

    private Vacancy getVacancy(String id) {
        EntityManager em = EMF.getEntityManager();

        try {
            Vacancy v = em.find(Vacancy.class, id);
            return v;
        } finally {
            em.close();
        }
    }

    public List<Vacancy> getVacancies() {
        if (vacancies.isEmpty()) return null;

        List<Vacancy> vacancyList = new ArrayList<Vacancy>();
        EntityManager em = EMF.getEntityManager();

        try {
            for (String id : vacancies) {
                if (id == null || id.isEmpty()) continue;
                Vacancy v = em.find(Vacancy.class, id);
                if (v != null) vacancyList.add(v);
            }
        } finally {
            em.close();
        }
        return vacancyList.isEmpty() ? null : vacancyList;
    }

    public boolean addVacancy(Vacancy aVacancy) {
        if (aVacancy == null) return false;
        String id = aVacancy.getId();
        if (id != null && !id.isEmpty()) return vacancies.add(id);
        else return false;
    }

    public boolean removeVacancy(Vacancy aVacancy) {
        if (aVacancy == null) return false;
        String id = aVacancy.getId();
        if (id != null && !id.isEmpty()) return vacancies.remove(id);
        else return false;
    }

    public void clearVacancies() {
        vacancies.clear();
    }

    public String getUrlinfoweb() {
        return urlinfoweb;
    }

    public void setUrlinfoweb(String urlinfoweb) {
        this.urlinfoweb = urlinfoweb;
    }

    public String getUrlinfoapi() {
        return urlinfoapi;
    }

    public void setUrlinfoapi(String urlinfoapi) {
        this.urlinfoapi = urlinfoapi;
    }

    public Contact(String userId, String name, String address, String organization, String phones, String emails, Boolean personflag) {
        this.name = name;
        this.address = address;
        this.organization = organization;
        this.phones = phones;
        this.emails = emails;
        this.personflag = personflag;
        comment = "";
        this.userId = userId;
    }

    public Contact(String userId, String name) {
        this(userId);
        this.name = name;
    }

    public Contact(Contact aContact) {

        if (aContact != null) {
            this.name = aContact.getName();
            this.address = aContact.getAddress();
            this.personflag = aContact.isPerson();
            this.organization = aContact.getOrganization();
            this.phones = aContact.getPhones();
            this.emails = aContact.getEmails();
            this.comment = aContact.getComment();
            this.userId = aContact.getUserId();
        }
    }

    public Contact(String userId) {
        name = "";
        address = "";
        personflag = true;
        organization = "";
        phones = "";
        emails = "";
        comment = "";
        this.userId = userId;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getAddress() {
        return address;
    }

    public String getOrganization() {
        return organization;
    }

    public void setOrganization(String organization) {
        this.organization = organization;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getName() {

        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPersonflag(Boolean personflag) {
        this.personflag = personflag;
    }

    public Boolean isPerson() {
        return personflag;
    }

    public String getPhones() {
        return phones;
    }

    public String getEmails() {
        return emails;
    }

    public String getId() {
        return id;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public void setPhones(String phones) {
        this.phones = phones;
    }

    public void setEmails(String emails) {
        this.emails = emails;
    }

    public Boolean getPersonflag() {
        return personflag;
    }
}
